package com.yanghongyue.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yanghongyue.reggie.bean.User;
import com.yanghongyue.reggie.common.R;
import com.yanghongyue.reggie.common.SMSUtils;
import com.yanghongyue.reggie.common.ValidateCodeUtils;
import com.yanghongyue.reggie.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author HongyueYang
 */
@RestController
@RequestMapping("/user")
public class UserCotroller {

    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate redisTemplate;

    //发送手机验证码
    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody User user, HttpServletRequest request){
        //获取手机号
        String phone = user.getPhone();
        //非空判断
        if(StringUtils.isNotEmpty(phone)) {
            //生成随机验证码
            Integer code = ValidateCodeUtils.generateValidateCode(4);
            System.out.println("生成的验证码===>" + code);
            //调用阿里云发送短信的方法
            //SMSUtils.sendMessage("ABC商城","SMS_463600053",phone,code+"");
            //将生成的验证码保存到session中
            //request.getSession().setAttribute("code",code+"");
            //将生成的验证码缓存到redis中，设置有效时间为5分钟
            redisTemplate.opsForValue().set(phone,code + "",5, TimeUnit.MINUTES);
            return R.sucess("验证码发送成功");
        }
        return R.error("验证码发送失败");
    }

    //登录
    @PostMapping("/login")
    public R<User> login(@RequestBody Map map, HttpServletRequest request){
        //获取参数
        String phone = map.get("phone").toString();
        String code = map.get("code").toString();
        //从session中获取保存的验证码
        //String sessionCode = (String) request.getSession().getAttribute("code");
        //从redis中获取缓存的验证码
        String sessionCode = (String) redisTemplate.opsForValue().get(phone);
        //校验验证码
        if (sessionCode!=null && sessionCode.equals(code)){
            //根据手机号查询User表
            QueryWrapper<User> wrapper = new QueryWrapper<>();
            wrapper.eq("phone",phone);
            User user = userService.getOne(wrapper);
            //如果用户不存在
            if(user==null){
                //注册用户
                user = new User();
                user.setPhone(phone);
                user.setStatus(1);
                //保存用户
                userService.save(user);
            }
            //将用户信息存入到session中
            request.getSession().setAttribute("userId",user.getId());
            //删除Redis中缓存的验证码
            redisTemplate.delete(phone);
            //返回用户信息
            return R.sucess(user);
        }else {
            return R.error("验证码错误");
        }
    }

    //退出登录
    @PostMapping("/loginout")
    public R<String> loginout(HttpServletRequest request){
        //清空session中保存的用户信息
        request.getSession().removeAttribute("userId");
        System.out.println("你好油桃");
        return R.sucess("退出成功");
    }
}
